package com.zry.fertilizer.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

public class ReadImgToBinary2 {

    public static String imgToBase64(String imgPath) {

        Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        //该方法用来压缩图片，第一个参数为图片格式，第二个参数为截取图片的保留率，如当前为90，则保留之前图片90%的区域
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

        byte[] imageByte = outputStream.toByteArray();

        return Base64.encodeToString(imageByte, Base64.DEFAULT);
    }

    private static Bitmap readBitmap(String imgPath) {
        try {
            return BitmapFactory.decodeFile(imgPath);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            return null;
        }

    }
}
